<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="available" >
        <el-select v-model="queryParams.available"  clearable @keyup.enter.native="handleQuery">
          <el-option value="" label="全部">全部</el-option>
          <el-option value="1" label="启用">启用</el-option>
          <el-option value="0" label="停用">停用</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:ProductCategory:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>



    <el-table
      v-loading="loading"
      :data="ProductCategoryList"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children'}">
      <el-table-column label="编号" align="center" prop="code" show-overflow-tooltip width="200px"/>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="进项税率" align="center" prop="taxRate" />
      <el-table-column label="销项税率" align="center" prop="saleTaxRate" />
      <el-table-column label="状态" align="center" prop="available">
        <template slot-scope="scope">
          <span>{{scope.row.available==1?"启用":"停用"}}</span >
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleChildredAdd(scope.row)"
          >新增子类目</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:ProductCategory:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增子类目 -->
    <el-dialog title="新增子类目" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="编号" prop="code">-->
<!--          <el-input v-model="form.code" placeholder="请输入编号" />-->
<!--        </el-form-item>-->
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>

        <el-form-item label="进项税率" prop="taxRate">
          <el-input v-model="taxRate" disabled />
        </el-form-item>

        <el-form-item label="销项税率" prop="saleTaxRate">
          <el-input v-model="saleTaxRate" disabled />
        </el-form-item>

        <el-form-item label="供应商" prop="supplierId">
          <el-input v-model="supplierName" disabled/>
        </el-form-item>

        <el-form-item label="上级类目" prop="parentId">
          <el-input v-model="parentName" disabled/>
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="form.description" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改商品类目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="编号" prop="code">-->
<!--          <el-input v-model="form.code" placeholder="请输入编号" />-->
<!--        </el-form-item>-->
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>

        <el-form-item label="进项税率" prop="taxRate" v-if="form.id == null">
          <el-input v-model="form.taxRate" placeholder="请输入进项税率" />
        </el-form-item>

        <el-form-item label="销项税率" prop="saleTaxRate" v-if="form.id == null">
          <el-input v-model="form.saleTaxRate" placeholder="请输入销项税率" />
        </el-form-item>

        <el-form-item label="状态" prop="available" v-if="form.id != null">
          <el-select v-model="form.available" @keyup.enter.native="handleQuery">
            <el-option :value="1" label="启用">启用</el-option>
            <el-option :value="0" label="停用">停用</el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="供应商" prop="supplierId" v-if="form.id == null">
          <el-select v-model="form.supplierId" clearable>
            <el-option v-for="suppliers in Supplier"
                       :key="suppliers.id"
                       :value="suppliers.id"
                       :label="suppliers.name">{{suppliers.name}}</el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="备注" prop="description">
          <el-input v-model="form.description" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductCategory, getProductCategory,listProductCategoryCode, delProductCategory, addProductCategory, updateProductCategory } from "@/api/system/ProductCategory";
//引入供应商
import {listSupplier} from "@/api/system/Supplier"
export default {
  name: "ProductCategory",
  dicts: ['sys_notice_status'],
  data() {
    return {
      //接收供应商数据
      Supplier:[],
      //编号验证
      codeNum:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品类目表格数据
      ProductCategoryList: [],
      // 商品类目未组装表格数据
      ProductCategoryCodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        available: null,
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 1000000,
      },
      // 表单参数
      form: {},
      //上级目录的编号与名称
      parentName:null,
      parentNameId:null,
      //上级目录的供应商名称与编号
      supplierId:null,
      supplierName:null,
      //进项税率
      taxRate:0,
      //销项税率
      saleTaxRate:0,
      // 表单校验
      rules: {
        code: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        available: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        supplierId: [
          { required: true, message: "供应商不能为空", trigger: "change" }
        ],
        taxRate: [
          { required: true, message: "进项税率不能为空", trigger: "blur" }
        ],
        saleTaxRate: [
          { required: true, message: "销项税率不能为空", trigger: "blur" }
        ],
        // description: [
        //   { required: true, message: "备注不能为空", trigger: "blur" }
        // ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        createById: [
          { required: true, message: "创建人ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" }
        ],
        updateById: [
          { required: true, message: "修改人ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getCodeList();
    this.getList();
    this.getsupplierList();
  },
  methods: {
    // 供应商
    getsupplierList(){
      this.loading = true;
      listSupplier(this.queryParams1).then(response => {
        this.Supplier = response.rows;
        this.loading = false;
      });
    },
    /** 查询商品类目列表 */
    getList() {
      this.loading = true;
      listProductCategory(this.queryParams).then(response => {
        this.ProductCategoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询商品类目列表 */
    getCodeList() {
      this.loading = true;
      listProductCategoryCode().then(response => {
        this.ProductCategoryCodeList = response.data;
        console.log(this.ProductCategoryCodeList);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        parentId: null,
        available: null,
        supplierId: null,
        taxRate: null,
        saleTaxRate: null,
        description: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleChildredAdd(row){
      this.reset();
      const id = row.id || this.ids
      getProductCategory(id).then(response => {
        this.parentName = response.data.name;
        this.parentNameId = response.data.id;
        this.supplierId = response.data.supplierId;
        this.supplierName = response.data.supplierName;
        this.taxRate = response.data.taxRate;
        this.saleTaxRate = response.data.saleTaxRate;
        this.open1 = true;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品类目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductCategory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品类目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductCategory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getCodeList();
            });
          } else {
            const Code = getOrderCode(this.ProductCategoryCodeList);
            this.form.code = Code;
            addProductCategory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getCodeList();
            });
          }
        }
      });
    },
    submitForm1(){
      this.form.parentId = this.parentNameId;
      this.form.supplierId = this.supplierId;
      this.form.taxRate = this.taxRate;
      this.form.saleTaxRate = this.saleTaxRate;
      const Code = getOrderCode(this.ProductCategoryCodeList);
      this.form.code = Code;
      addProductCategory(this.form).then(response => {
        this.$modal.msgSuccess("新增子类目成功");
        this.open1 = false;
        this.getList();
        this.getCodeList();
      });
    }
  }
};
//单号生成
function getOrderCode(list) {
  var date = new Date(),
    year = date.getFullYear(),
    month = date.getMonth() + 1, // 月份是从0开始的
    day = date.getDate();

// 如果月份或日期是个位数，在前面补0
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;

  var time=year + '' + month + '' + day

  let type="SPLM"; //商品类目
  var code=time+'0000000'
  var str1=code.slice(0,7)
  var str2=code.slice(7,)

  if (str2.slice(0,1)=="0"){
    str1=code.slice(0,6)
    str2=code.slice(6,)

    for (var i=0;i<list.length;i++){
      let listCode=list[i].code.slice(4,);
      if (listCode.slice(0, 8)===time){
        if (parseInt(listCode.slice(6,))>parseInt(str2)){
          code=listCode;
          str2=code.slice(6,)
        }
      }
    }
  }else {
    for (var i=0;i<list.length;i++){
      let listCode=list[i].code.slice(4,);
      if (listCode.slice(0, 8)===time){
        if (parseInt(listCode.slice(7,))>parseInt(str2)){
          code=listCode;
          str2=code.slice(7,)
        }
      }
    }
  }

  code=type+str1+(parseInt(str2)+1)

  return code.toString();
}
</script>
